using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;

namespace Microsoft.DesignerSupport
{
	public struct XsdConstants
	{
		internal const string Ns = "http://schemas.microsoft.com/VisualStudio/2004/03/SmartDevices/XMTA.xsd";
	}

	[XmlRoot("Classes", Namespace = XsdConstants.Ns, IsNullable = false)]
	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class NetCFMetadata
	{
		[XmlElement("Class")]
		public List<ClassType> m_classCollection;

		[XmlIgnore()]
		public List<ClassType> Class
		{
			get
			{
				if ((this.m_classCollection == null))
					this.m_classCollection = new List<ClassType>();
				return this.m_classCollection;
			}
			set
			{
				this.m_classCollection = value;
			}
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class ClassType
	{
		public ClassType() { }
		public ClassType(string name)
		{
			Name = name;
		}

		internal bool HasData()
		{
			/*return (this.ApplyDeviceDefaults.TrueForAll(delegate(ApplyDeviceDefaultsType t) { return t.HasData(); }) ||
				this.Event.TrueForAll(delegate(EventType t) { return t.HasData(); }) ||
				this.Method.TrueForAll(delegate(MethodType t) { return t.HasData(); }) ||
				this.Property.TrueForAll(delegate(PropertyType t) { return t.HasData(); }) ||*/
			return ((this.m_ApplyDeviceDefaults != null && this.m_ApplyDeviceDefaults.Count > 0) ||
				(this.m_Event != null && this.m_Event.Count > 0) ||
				(this.m_Method != null && this.m_Method.Count > 0) ||
				(this.m_propertyCollection != null && this.m_propertyCollection.Count > 0) ||
				!string.IsNullOrEmpty(DefaultEvent) ||
				!string.IsNullOrEmpty(DefaultProperty) ||
				!string.IsNullOrEmpty(Description) ||
				!string.IsNullOrEmpty(InitializationEvent) ||
				!string.IsNullOrEmpty(DefaultBindingProperty) ||
				!string.IsNullOrEmpty(Docking) ||
				!string.IsNullOrEmpty(ToolboxBitmap) ||
				this.DesktopCompatible.HasValue ||
				this.Supported.HasValue ||
				this.SuppressFiltering.HasValue ||
				this.DesignTimeVisible.HasValue ||
				this.ImmutableObject.HasValue ||
				(this.TypeConverter != null && this.TypeConverter.HasData()) ||
				(this.DesignerSerializer != null && this.DesignerSerializer.HasData()) ||
				(this.RootDesignerSerializer != null && this.RootDesignerSerializer.HasData()) ||
				(this.Designer != null && this.Designer.HasData()) ||
				(this.Editor != null && this.Editor.HasData()) ||
				(this.ComplexBindingProperties != null && this.ComplexBindingProperties.HasData()) ||
				(this.LookupBindingProperties != null && this.LookupBindingProperties.HasData())
				);
		}

		[XmlAttribute]
		public string Name;

		[XmlElement("Property")]
		public List<PropertyType> m_propertyCollection;

		[XmlIgnore()]
		public List<PropertyType> Property
		{
			get
			{
				if ((this.m_propertyCollection == null))
				{
					this.m_propertyCollection = new List<PropertyType>();
				}
				return this.m_propertyCollection;
			}
			set
			{
				this.m_propertyCollection = value;
			}
		}

		[XmlElement("Event")]
		public List<EventType> m_Event;

		[XmlIgnore]
		public List<EventType> Event
		{
			get
			{
				if ((this.m_Event == null))
				{
					this.m_Event = new List<EventType>();
				}
				return this.m_Event;
			}
			set
			{
				this.m_Event = value;
			}
		}

		[XmlElement("Method")]
		public List<MethodType> m_Method;

		[XmlIgnore]
		public List<MethodType> Method
		{
			get
			{
				if ((this.m_Method == null))
				{
					this.m_Method = new List<MethodType>();
				}
				return this.m_Method;
			}
			set
			{
				this.m_Method = value;
			}
		}

		public string DefaultEvent;
		public string DefaultProperty;
		public string Description;
		public bool? Supported;
		[XmlIgnore]
		public bool SupportedSpecified { get { return this.Supported.HasValue; } set { if (!value) this.Supported = null; } }
		public bool? SuppressFiltering;
		[XmlIgnore]
		public bool SuppressFilteringSpecified { get { return this.SuppressFiltering.HasValue; } set { if (!value) this.SuppressFiltering = null; } }
		public typedeftype TypeConverter;
		public string InitializationEvent;
		public bool? DesignTimeVisible;
		[XmlIgnore]
		public bool DesignTimeVisibleSpecified { get { return this.DesignTimeVisible.HasValue; } set { if (!value) this.DesignTimeVisible = null; } }
		public typedeftype DesignerSerializer;
		public typedeftype RootDesignerSerializer;
		public bool? ImmutableObject;
		[XmlIgnore]
		public bool ImmutableObjectSpecified { get { return this.ImmutableObject.HasValue; } set { if (!value) this.ImmutableObject = null; } }
		public typedeftype Designer;
		public typedeftype Editor;
		public string DefaultBindingProperty;
		public complexdatabinding ComplexBindingProperties;
		public lookupdatabinding LookupBindingProperties;
		public bool? DesktopCompatible;
		[XmlIgnore]
		public bool DesktopCompatibleSpecified { get { return this.DesktopCompatible.HasValue; } set { if (!value) this.DesktopCompatible = null; } }

		[XmlElement("ApplyDeviceDefaults")]
		public List<ApplyDeviceDefaultsType> m_ApplyDeviceDefaults;
		[XmlIgnore()]
		public List<ApplyDeviceDefaultsType> ApplyDeviceDefaults
		{
			get
			{
				if ((this.m_ApplyDeviceDefaults == null))
				{
					this.m_ApplyDeviceDefaults = new List<ApplyDeviceDefaultsType>();
				}
				return this.m_ApplyDeviceDefaults;
			}
			set
			{
				this.m_ApplyDeviceDefaults = value;
			}
		}

		public string Docking;
		public string ToolboxBitmap;
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class ApplyDeviceDefaultsType
	{
		public string ComponentName;
		public string PropertyName;
		public bool? ApplyDefaults;
		[XmlIgnore]
		public bool ApplyDefaultsSpecified { get { return this.ApplyDefaults.HasValue; } set { if (!value) this.ApplyDefaults = null; } }

		internal bool HasData()
		{
			return (!string.IsNullOrEmpty(this.ComponentName) ||
				!string.IsNullOrEmpty(this.PropertyName) ||
				this.ApplyDefaults.HasValue
				);
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class lookupdatabinding
	{
		public string DataSource;
		public string DisplayMember;
		public string LookupMember;
		public string ValueMember;

		internal bool HasData()
		{
			return (!string.IsNullOrEmpty(this.DataSource) ||
				!string.IsNullOrEmpty(this.DisplayMember) ||
				!string.IsNullOrEmpty(this.LookupMember) ||
				!string.IsNullOrEmpty(this.ValueMember)
				);
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class complexdatabinding
	{
		public string DataMember;
		public string DataSource;

		internal bool HasData()
		{
			return (!string.IsNullOrEmpty(this.DataSource) ||
				!string.IsNullOrEmpty(this.DataMember)
				);
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class MethodType
	{
		public MethodType() { }
		public MethodType(string name)
		{
			Name = name;
		}

		public string EditorBrowsable;
		public bool? Supported;
		[XmlIgnore]
		public bool SupportedSpecified { get { return this.Supported.HasValue; } set { if (!value) this.Supported = null; } }
		[XmlAttribute]
		public string Name;
		public string Parameters;

		internal bool HasData()
		{
			return (!string.IsNullOrEmpty(this.EditorBrowsable) ||
				!string.IsNullOrEmpty(this.Parameters) ||
				this.Supported.HasValue
				);
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class EventType
	{
		public EventType() { }
		public EventType(string name)
		{
			Name = name;
		}

		public bool? Browsable;
		[XmlIgnore]
		public bool BrowsableSpecified { get { return this.Browsable.HasValue; } set { if (!value) this.Browsable = null; } }
		public string Category;
		public string Description;
		public string EditorBrowsable;
		[XmlAttribute]
		public string Name;
		public bool? Supported;
		[XmlIgnore]
		public bool SupportedSpecified { get { return this.Supported.HasValue; } set { if (!value) this.Supported = null; } }

		internal bool HasData()
		{
			return (!string.IsNullOrEmpty(this.Category) ||
				!string.IsNullOrEmpty(this.Description) ||
				!string.IsNullOrEmpty(this.EditorBrowsable) ||
				this.Browsable.HasValue ||
				this.Supported.HasValue
				);
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class typedeftype
	{
		[XmlIgnore]
		private Type m_type;

		public typedeftype() { }

		public static typedeftype FromString(string td)
		{
			Type t = Helper.StringToType(td);
			return (t == null) ? null : new typedeftype(t);
		}

		public typedeftype(Type t)
		{
			m_type = t;
		}

		public string Type
		{
			get { return m_type == null ? null : Helper.GetTypeString(m_type); }
			set { if (string.IsNullOrEmpty(value)) m_type = null; else m_type = System.Type.GetType(value, false, true); }
		}

		public string BaseType
		{
			get { return m_type == null ? null : Helper.GetTypeString(m_type.BaseType); }
			set { }
		}

		internal bool HasData()
		{
			return m_type != null;
		}

		public override string ToString()
		{
			return this.Type;
		}
	}

	internal static class Helper
	{
		public static string GetTypeString(System.Type t)
		{
			if (t.Assembly.GetName().Name.Equals("mscorlib"))
				return t.FullName;
			return t.AssemblyQualifiedName;
		}

		public static Type StringToType(string td)
		{
			Type t = null;
			if (string.IsNullOrEmpty(td) || (t = Type.GetType(td, false, true)) == null)
				return null;
			return t;
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class objecttype
	{
		[XmlIgnore]
		private object m_obj;
		[XmlIgnore]
		private Type m_type;
		[XmlIgnore]
		public Type InternalType
		{
			get { return m_type; }
			set
			{
				m_type = value;
				if (m_type != null && m_obj != null && !m_obj.GetType().Equals(m_type))
				{
					if (m_type != typeof(string) && m_obj is string && m_obj.ToString() == string.Empty)
						m_obj = null;
					else
					{
						try
						{
							TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(m_type);
							if (tc != null)
								m_obj = tc.ConvertFrom(m_obj);
						}
						catch { }
					}
				}
			}
		}

		public objecttype() { }

		public objecttype(object o)
		{
			m_obj = o;
			m_type = o.GetType();
		}

		public objecttype(Type t)
		{
			m_obj = null;
			m_type = t;
		}

		public string Type
		{
			get { return m_obj == null ? null : Helper.GetTypeString(m_obj.GetType()); }
			set
			{
				if (string.IsNullOrEmpty(value))
					m_type = null;
				else
				{
					m_type = System.Type.GetType(value, false, true);
					if (m_type != null)
					{
						if (m_type.Equals(m_obj))
							return;
						if (m_obj != null && m_obj is string)
							ConvertFromString((string)m_obj);
					}
				}
			}
		}

		public string Value
		{
			get
			{
				if (m_obj != null)
				{
					TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(m_obj);
					if (tc != null)
						return tc.ConvertToString(m_obj);
					return m_obj.ToString();
				}
				return null;
			}
			set
			{
				m_obj = value;
				if (m_obj == null)
					m_type = null;
				else
					ConvertFromString(value);
			}
		}

		private void ConvertFromString(string value)
		{
			if (m_type != null)
			{
				TypeConverter tc = System.ComponentModel.TypeDescriptor.GetConverter(m_type);
				if (tc != null && !string.IsNullOrEmpty(value))
					m_obj = tc.ConvertFromString(value);
			}
		}

		internal bool HasData()
		{
			return m_obj != null;
		}

		public override string ToString()
		{
			return this.Value;
		}
	}

	[XmlType(Namespace = XsdConstants.Ns)]
	public partial class PropertyType
	{
		public PropertyType() { }
		public PropertyType(string name)
		{
			Name = name;
		}

		// Fields
		public objecttype AmbientValue;
		[XmlIgnore]
		public bool AmbientValueSpecified { get { return this.AmbientValue != null && !string.IsNullOrEmpty(this.AmbientValue.Value); } set { if (!value) this.AmbientValue = null; } }
		public bool? Bindable;
		[XmlIgnore]
		public bool BindableSpecified { get { return this.Bindable.HasValue; } set { if (!value) this.Bindable = null; } }
		public bool? Browsable;
		[XmlIgnore]
		public bool BrowsableSpecified { get { return this.Browsable.HasValue; } set { if (!value) this.Browsable = null; } }
		public string Category;
		public objecttype DefaultValue;
		[XmlIgnore]
		public bool DefaultValueSpecified { get { return this.DefaultValue != null && !string.IsNullOrEmpty(this.DefaultValue.Value); } set { if (!value) this.DefaultValue = null; } }
		public string Description;
		public string DesignerSerializationVisibility;
		public bool? DesignOnly;
		[XmlIgnore]
		public bool DesignOnlySpecified { get { return this.DesignOnly.HasValue; } set { if (!value) this.DesignOnly = null; } }
		public typedeftype Editor;
		public string EditorBrowsable;
		public bool? ListBindable;
		[XmlIgnore]
		public bool ListBindableSpecified { get { return this.ListBindable.HasValue; } set { if (!value) this.ListBindable = null; } }
		public bool? Localizable;
		[XmlIgnore]
		public bool LocalizableSpecified { get { return this.Localizable.HasValue; } set { if (!value) this.Localizable = null; } }
		public bool? MergableProperty;
		[XmlIgnore]
		public bool MergablePropertySpecified { get { return this.MergableProperty.HasValue; } set { if (!value) this.MergableProperty = null; } }
		[XmlAttribute]
		public string Name;
		public bool? NotifyParentProperty;
		[XmlIgnore]
		public bool NotifyParentPropertySpecified { get { return this.NotifyParentProperty.HasValue; } set { if (!value) this.NotifyParentProperty = null; } }
		public bool? ParenthesizeProperty;
		[XmlIgnore]
		public bool ParenthesizePropertySpecified { get { return this.ParenthesizeProperty.HasValue; } set { if (!value) this.ParenthesizeProperty = null; } }
		public bool? ReadOnly;
		[XmlIgnore]
		public bool ReadOnlySpecified { get { return this.ReadOnly.HasValue; } set { if (!value) this.ReadOnly = null; } }
		public string RefreshProperties;
		public string RelatedImageList;
		public bool? Supported;
		[XmlIgnore]
		public bool SupportedSpecified { get { return this.Supported.HasValue; } set { if (!value) this.Supported = null; } }
		public bool? SuppressFiltering;
		[XmlIgnore]
		public bool SuppressFilteringSpecified { get { return this.SuppressFiltering.HasValue; } set { if (!value) this.SuppressFiltering = null; } }
		public typedeftype TypeConverter;

		internal bool HasData()
		{
			return (!string.IsNullOrEmpty(this.Category) ||
				!string.IsNullOrEmpty(this.Description) ||
				!string.IsNullOrEmpty(this.DesignerSerializationVisibility) ||
				!string.IsNullOrEmpty(this.EditorBrowsable) ||
				!string.IsNullOrEmpty(this.RefreshProperties) ||
				!string.IsNullOrEmpty(this.RelatedImageList) ||
				this.Bindable.HasValue ||
				this.Browsable.HasValue ||
				this.DesignOnly.HasValue ||
				this.ListBindable.HasValue ||
				this.MergableProperty.HasValue ||
				this.NotifyParentProperty.HasValue ||
				this.ParenthesizeProperty.HasValue ||
				this.ReadOnly.HasValue ||
				this.Supported.HasValue ||
				this.SuppressFiltering.HasValue ||
				(this.TypeConverter != null && this.TypeConverter.HasData()) ||
				(this.Editor != null && this.Editor.HasData()) ||
				(this.AmbientValue != null && this.AmbientValue.HasData()) ||
				(this.DefaultValue != null && this.DefaultValue.HasData())
				);
		}
	}
}
